<%@page import="cz.tpj.logic.*" %>
<%@page import="cz.tpj.database.bod_zaujmu.Bod_zaujmu"%>
<%@page import="cz.tpj.util.attractive_place.AttractivePlace"%>
<%@page import="cz.tpj.util.ErrorDatabase"%>
<%@page import="cz.tpj.database.bod_zaujmu.Table_Bod_zaujmu"%>
<%@page import="cz.tpj.database.zamestnanec.Zamestnanec"%>
<%@page import="cz.tpj.database.mesto.Table_city"%>
<%@page import="java.util.ArrayList"%>
<%@page import="cz.tpj.database.mesto.City"%>
<%@page import="java.util.Iterator"%>
<%@page import="cz.tpj.database.aktivity.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
//---inicializace---
final String ACTION = "action";
String message = new String();
Bod_zaujmu actualPlace = null;
//inicializace farmularovych hodnot
String name = "", shortDescr = "", longDescr = "", cityId = "", gps = "";
//
final String SELECTED_ACTIVITIES = "selected_activities[]";
//nacte vsechny vybrane aktivity
String[] selectedAct = request.getParameterValues(SELECTED_ACTIVITIES);
//---zpracovani formulare---
String action = request.getParameter(ACTION);
//pokud bylo zmacknuto tlacitko Uloz, pak proved nasledujici kod
if(action != null){
	//---nacteni udaju z formulare a ulozeni do session jako ACTUAL_ATTRACTIVE...---
	boolean ok = AttractivePlace.getInstance().fetchPlace(request);
	//ze session nacte data opet pro formular
	actualPlace = (Bod_zaujmu) session.getAttribute(AttractivePlace.ACTUAL_PLACE_INSTANCE);
	//formulareve promenne
	name 		= actualPlace.getNazov();
	shortDescr 	= actualPlace.getKratkyPopis();
	longDescr 	= actualPlace.getDlhyPopis();
	cityId		= actualPlace.getMesto();
	gps			= actualPlace.getGps();
	//ulozi pouze pokud bude spravne vyplneny nazev mista
	if(ok){
		//message = "<p>Vporadku uloz...</p>";
		try{
			//zapsani mista do databaze
			ok = Table_Bod_zaujmu.getInstance().insertBodZaujmu(actualPlace);
			//nacte id posledne pridaneho Bodu zajmu
			String lastIdInterest = Table_Bod_zaujmu.getInstance().getLastInsertedIterestPlace();
			//pokud je nejaky vybran, pak jej ulozi do databaze
			if(selectedAct != null)
				message += TableActivityInterests.getInstance().addAllActivitiesToInterest(selectedAct, lastIdInterest);
			//1) probehlo vse v poradku
			//2) nebylo nic zapsano - misto s timto jmenem totiz uz existuje
			if(ok){
				message += "<p>Zajímavé místo " + name + " je uloženo.</p>";
				//vycisti promenne a session
				name = shortDescr = longDescr = cityId = "";
				session.setAttribute(AttractivePlace.ACTUAL_PLACE_INSTANCE, null);
				response.sendRedirect(Navigation.JSP_ATTRACTIVE_PLACE);
			}else{
				message += "<p>Zajímevé místo s tímto názvem v databázi zřejmně již existuje!</p>";
			}
		}catch(ErrorDatabase e){
			message += "<p>" + e.getMessage() + "</p>";
		}
	}else{
		message += (String) session.getAttribute(AttractivePlace.MESSAGE);
	}
}
%>


<%@page import="cz.tpj.database.aktivita_zajimavost.TableActivityInterests"%><html>
    <%= Navigation.getHead(null) %>
    <%= Navigation.getStartBody() %>
    <%= Navigation.getNavigationPanel(null, request) %>
          
        <div id="<%= Navigation.DIV_CENTER %>">
            <h1>Přidej bod zájmu</h1>
            <%= message %>
            <form accept-charset="utf-8" method="POST" action="<%= Navigation.JSP_ADD_ATTRACTIVE_PLACE %>">
	            <p>
	            	<label>Název místa:</label><br/>
	           		<input type="text" name="<%= AttractivePlace.NAME %>" value="<%= name %>">
	           	</p>
	           	<p>
	           		<label>Krátký popis:</label><br/>
	           		<textarea name="<%= AttractivePlace.SHORT_DESCR %>" cols="50" rows="5"><%= shortDescr %></textarea>
	           	</p>
	           	<p>
	           		<label>Dlouhý popis:</label><br/>
	           		<textarea name="<%= AttractivePlace.LONG_DESCR %>" cols="50" rows="10"><%= longDescr %></textarea>
	           	</p>
	           	<p>
	           		<label>Souřadni GPS (formát podle mapy.cz)</label><br/>
	           		<!-- <input type="text" name="<%= AttractivePlace.GPS %>" value="<%= gps %>"> -->
	           		<textarea name="<%= AttractivePlace.GPS %>" cols="50" rows="1"><%= gps %></textarea>
	           	</p>
	           	<p>
	           		<label>Přiřaď městu:</label><br/>
	           		<select name="<%= AttractivePlace.CITY_ID %>" >
	           			<option value="">Vyber město</option>
	           			<%
	           			//nacti vsechny mesta z databaze
	           			Iterator<City> cities = null;
	           			try{
	           				cities = Table_city.getInstance().getAllCity().iterator();
	           			}catch(ErrorDatabase e){
	           				message += e.getMessage();
	           			}
	           			//zobraz je
	           			City actualCity = null;
	           			while(cities.hasNext()){
	           				actualCity = cities.next();
	           				String c = actualCity.getId();
	           				if(c != null && c.equals(cityId)){
	           					out.println("<option selected value='" + actualCity.getId() + "'>" + actualCity.getName() + "</option>");
	           				}else{
	           					out.println("<option value='" + actualCity.getId() + "'>" + actualCity.getName() + "</option>");
	           				}
	           			}
	           			%>
	           		</select>
	           	</p>
	           	<p>
	           	<%
				//nacti vsechny aktivity
				ArrayList<Aktivita> activitiesNames = Table_Aktivita.getInstance().getAktivity();
				Iterator<Aktivita> it = activitiesNames.iterator();
				Aktivita a;
				//projdi jejich nazvy a zobraz je jako checkBoxy
				while(it.hasNext()){
					//dalsi aktivita
					a = it.next();
					//1) aktivita byla drive vybrana, pak ji nastav jako zaskrtnutou
					//2) aktivita nebyla vybrana, pak ji nezaskrtavej
					if(a.imSelected(selectedAct)){
						%>
						<input type="checkbox" 
							   name="<%= SELECTED_ACTIVITIES %>" 
							   value="<%= a.getId() %>" checked/><%= a.getNazov() %><br/>
						<%
					}else{
						%>
						<input type="checkbox" 
							   name="<%= SELECTED_ACTIVITIES %>" 
							   value="<%= a.getId() %>" /><%= a.getNazov() %><br/>
						<%	
					}
				}
				%>
	           	</p>
	           	<p>
	           		<input type="submit" name="<%= ACTION %>" value="Ulož">
	          	</p>
          	</form>
        </div>
 	
 	<%= Navigation.getLoginLink(request) %>
</html>
